package com.qubuyer.business.main

import android.os.Bundle
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.BarUtils
import com.qubuyer.R
import com.qubuyer.core.app.DataBindingActivity
import com.qubuyer.databinding.ActivityMainBinding
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class MainActivity : DataBindingActivity<ActivityMainBinding>() {

    companion object {
        private const val FRAGMENT_KEY_MAIN = "FRAGMENT_KEY_MAIN"
    }

    private var mIsExit = false

    private var mainFragment: MainFragment? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (savedInstanceState != null) {
            mainFragment = supportFragmentManager.getFragment(savedInstanceState, FRAGMENT_KEY_MAIN) as MainFragment?
        }
        if (mainFragment == null) {
            mainFragment = MainFragment()
            addContentFragment(mainFragment!!, R.id.flContent)
        } else {
            addFragmentLifecycleCallbacks()
        }
    }

    override fun onResume() {
        super.onResume()
        BarUtils.setStatusBarLightMode(this, true)
    }

    override fun onBackPressed() {
        if (mIsExit) {
            ActivityUtils.finishAllActivities()
        } else {
            toast("再按一次退出")
            mIsExit = true
            launch {
                delay(2000)
                mIsExit = false
            }
        }
    }

    override fun onDataBind(dataBinding: ActivityMainBinding) {
        dataBinding.host = this
    }

    override val layoutId: Int
        get() = R.layout.activity_main
}